home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / TSKRSC.CPP < prev    next >
C/C++ Source or Header  |  1991-08-21  |  3KB  |  160 lines

  1. /*
  2.    CPPTask - A Multitasking Kernel For C++
  3.  
  4.    Version 1.0 08-12-91
  5.  
  6.    Ported by Rich Smith from:
  7.  
  8.    Public Domain Software written by
  9.       Thomas Wagner
  10.       Patschkauer Weg 31
  11.       D-1000 Berlin 33
  12.       West Germany
  13.  
  14.    TSKRSC.CPP - Resource handling routines.
  15.  
  16.    Subroutines:
  17.         resource::resource
  18.         resource::~resource
  19.         resource::request_resource
  20.         resource::c_request_resource
  21.         resource::check_resource
  22.         resource::get_state
  23.         resource::get_flags
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. #include "task.hpp"
  30. #include "tsklocal.hpp"
  31.  
  32. /*
  33.    resource - initialise resource.
  34. */
  35.  
  36. resource::resource ()
  37. {
  38.   flags = 0;
  39.  
  40.   waiting = NULL;
  41.   state = 1;
  42.   owner = NULL;
  43. }
  44.  
  45. /*
  46.    ~resource - Kill all tasks waiting for the resource.
  47. */
  48.  
  49. resource::~resource ()
  50. {
  51.    CRITICAL;
  52.  
  53.    C_ENTER;
  54.    tsk_kill_queue (&waiting);
  55.    owner = NULL;
  56.    state = 0;
  57.    C_LEAVE;
  58.  
  59. }
  60.  
  61.  
  62. /*
  63.    request_resource - Wait until resource is available. If the resource
  64.                       is free on entry, it is assigned to the task, and
  65.                       the task continues to run.
  66.                       If the task requesting the resource already owns it,
  67.                       this routine returns 0.
  68. */
  69.  
  70. int far resource::request_resource (dword timeout)
  71. {
  72.    CRITICAL;
  73.  
  74.    C_ENTER;
  75.    if (state || owner == tsk_current)
  76.       {
  77.       state = 0;
  78.       owner = tsk_current;
  79.       C_LEAVE;
  80.       return 0;
  81.       }
  82.    tsk_current->set_retptr(NULL);
  83.    tsk_wait (&waiting, timeout);
  84.    return (int)tsk_current->get_retptr();
  85. }
  86.  
  87.  
  88. /*
  89.    c_request_resource - If the resource is free on entry, it is assigned 
  90.                         to the task, otherwise an error status is returned.
  91. */
  92.  
  93. int far resource::c_request_resource (void)
  94. {
  95.    int rv;
  96.    CRITICAL;
  97.  
  98.    C_ENTER;
  99.    if (rv = state)
  100.       {
  101.       state = 0;
  102.       owner = tsk_current;
  103.       }
  104.    C_LEAVE;
  105.    return (rv) ? 0 : -1;
  106. }
  107.  
  108.  
  109. /*
  110.    release_resource - Release the resource. If there are tasks waiting for
  111.                       the resource, it is assigned to the first waiting
  112.                       task, and the task is made eligible.
  113. */
  114.  
  115. void far resource::release_resource (void)
  116. {
  117.    tcbptr curr;
  118.    CRITICAL;
  119.  
  120.    if (owner != tsk_current)
  121.       return;
  122.  
  123.    C_ENTER;
  124.    if ((curr = waiting) == NULL)
  125.       {
  126.       state = 1;
  127.       owner = NULL;
  128.       C_LEAVE;
  129.       return;
  130.       }
  131.  
  132.    waiting = curr->tsk_runable ();
  133.    owner = curr;
  134.    C_LEAVE;
  135. }
  136.  
  137.  
  138. /*
  139.    check_resource - returns 1 if the resource is available.
  140. */
  141.  
  142. int far resource::check_resource (void)
  143. {
  144.    return state;
  145. }
  146.  
  147.  
  148. int far asm_request_resource(resourceptr rsc, dword timeout)
  149. {
  150.     return rsc->request_resource(timeout);
  151. }
  152.  
  153.  
  154.  
  155. void far asm_release_resource(resourceptr rsc)
  156. {
  157.     rsc->release_resource();
  158. }
  159.  
  160.